package com.example.naodian.utiles;

import com.example.naodian.domain.po.Electrode;
import com.example.naodian.mapper.ElectrodeMapper;
import com.example.naodian.service.IElectrodeService;
import com.example.naodian.service.impl.ElectrodeServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 拷贝excel的 脑点点位名称到数据库
 */
@Component
public class CsvImportUtil {

    @Autowired
    private IElectrodeService electrodeService;

    public void importElectrodeNamesFromCsv(String filePath) {
        List<Electrode> electrodes = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String firstLine = br.readLine();
            if (firstLine != null) {
                String[] names = firstLine.split(",");
                for (int i = 1; i < names.length; i++) {
                    String name = names[i].trim();
                    if (!name.isEmpty()) {
                        Electrode electrode = new Electrode();
                        electrode.setName(name);
                        electrodes.add(electrode);
                    }
                }
            }
            // 批量插入数据
            if (!electrodes.isEmpty()) {
                electrodeService.saveBatch(electrodes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
